package leetcode.all;

import java.util.HashSet;

public class Solution3 {

    /**
     * 练习滑动窗口
     * @param s
     * @return
     */
    public int lengthOfLongestSubstring(String s) {

        int res = 0, left = 0;
        HashSet<Character> hashSet = new HashSet<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            //先尝试进，无法进则左出
            while (!hashSet.add(c)) hashSet.remove(s.charAt(left++));
            //计算
            res = Math.max(res,i-left+1);
        }
        return res;
    }
}
